<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Activity extends MY_Admin_Controller {

	public function __construct()
    {
        parent::__construct();
        $this->load->model('Activity_model');
    }
	
    //默认执行index
	public function index()
	{
		$type = $this->input->get_post('type');
		$field = $this->input->get_post('field');
		$cKey = $this->input->get_post('txtKey');
		$fieldDate = $this->input->get_post('fieldDate');
		$begtime = $this->input->get_post('begtime');
		$endtime = $this->input->get_post('endtime');
		$orderby = $this->input->get_post('orderby');

		if($field=='ti_tle')
			$field = 'title';

		$dbprefix = $this->Activity_model->db->dbprefix;
		$page     = _get_page();
		$pagesize = 10;
		$arrParam = array();
		$arrWhere = array();

		if($type)
		{
			$arrParam['type'] = $type;
			$arrWhere['type'] = $type;
		}
		
		if($cKey)
		{
			$arrParam['key'] = $cKey;
			if($field=='userid')
				$arrWhere[$field] = $cKey;
			else
				$arrWhere[$field.' like '] = "'%$cKey%'";
		}
		$arrParam['field'] = $field;
		$arrParam['fieldDate'] = $fieldDate;

		if($begtime)
		{
			$arrParam['begtime'] = $begtime;
			$arrWhere["a.$fieldDate >="] = strtotime($begtime);
		}
		if($endtime)
		{
			$arrParam['endtime'] = $endtime;
			$arrWhere["a.$fieldDate <="] = strtotime("$endtime 23:59:59");
		}
		$strOrder = 'a.id desc';
		if($orderby)
		{
			$arrParam['orderby'] = $orderby;
			$strOrder = $orderby;
		}

		$tb = $dbprefix.'activity a left join '.$dbprefix.'recommend b on(b.outerid=a.id and b.kind=2) left join '.$dbprefix.'fans_recommend c on(c.outerid=a.id and c.fans_kind=2)';
		$list = $this->Activity_model->fetch_page($page, $pagesize, $arrWhere, 'a.*,(b.id is not null) as isrecommend,(c.id is not null) as isfansrecommend', $strOrder,$tb);
		//echo $this->db->last_query();die;
		$oSysActType = _get_config('activity');

		//分页
		$pagecfg = array();
		$pagecfg['base_url']     = _create_url('admin/activity', $arrParam);
		$pagecfg['total_rows']   = $list['count'];
		$pagecfg['cur_page'] = $page;
		$pagecfg['per_page'] = $pagesize;
// 		$this->load->library('pagination');
		$this->pagination->initialize($pagecfg);
		$list['pages'] = $this->pagination->create_links();

		$result = array(
			'list' => $list,
			'oSysActType' => $oSysActType,
			'arrParam' => $arrParam,
			);


		$this->load->view('admin/activity',$result);
	}

	public function add()
	{
		//需要修改
		$id = _get_key_val($this->input->get('id'), TRUE);
		$result = array();
		
		// 没有有提交
		if ( $this->input->is_post() )
		{
			// 设置规则
			$config = array(
				array(
					'field' => 'title',
					'lable' => '通告名称',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'type',
					'label' => '通告性质',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'endtime',
					'label' => '报名截止时间',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'img',
					'label' => '通告封面',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'cost',
					'label' => '费用预算',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'category',
					'label' => '工作性质',
					'rules' => 'trim|required'
				)
			);
			// 工作
			$workConfig = array(
				array(
					'field' => 'begtime',
					'label' => '工作时间',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'place',
					'label' => '工作地点',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'sex',
					'label' => '性别',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'people_num',
					'label' => '人数',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'height',
					'label' => '身高',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'style',
					'label' => '风格',
					'rules' => 'trim|required'
				)
			);
			// 面试
			$interviewConfig = array(
				array(
					'field' => 'address',
					'label' => '面试地点',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'interview_begin_time',
					'label' => '面试时间',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'summary',
					'label' => '模特要求',
					'rules' => 'trim|required'
				),
// 				array(
// 					'field' => 'sex',
// 					'label' => '性别',
// 					'rules' => 'trim|required'
// 				),
// 				array(
// 					'field' => 'people_num',
// 					'label' => '人数',
// 					'rules' => 'trim|required'
// 				),
// 				array(
// 					'field' => 'height',
// 					'label' => '身高',
// 					'rules' => 'trim|required'
// 				),
// 				array(
// 					'field' => 'style',
// 					'label' => '风格',
// 					'rules' => 'trim|required'
// 				)
			);
			// 制作通告
			$gameConfig = array(
				array(
					'field' => 'begtime',
					'label' => '工作开始时间',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'place',
					'label' => '工作地点',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'm_content',
					'label' => '制作要求',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'm_num',
					'label' => '人数',
					'rules' => 'trim|required'
				)
			);
			
			$this->form_validation->set_rules($config);
			if ( $this->form_validation->run() === TRUE )
			{
				$data = array(
					'title' => $this->input->post('title'),
					'type' => $this->input->post('type'),
					'inendtime' => strtotime($this->input->post('endtime')),
					'workfee' => $this->input->post('cost'),
					'img' => $this->input->post('img'),
					'status' => 1,
					'op_userid' => $this->session->userdata['admin_id'],
					'op_username' => $this->session->userdata['user_name'],
					'op_time' => time(),
					'content' => $this->input->post('introduction'),
					'other' => $this->input->post('other'),
					'actnum' => (int)$this->input->post('actnum'),
					'innumfake' => (int)$this->input->post('innumfake'),
					'innum' => (int)$this->input->post('innum'),
					'display' => $this->input->post('display')
				);
				switch ($data['type'])
				{
					case '3':
						// 制作通告
						$this->form_validation->set_rules($gameConfig);
						if ( $this->form_validation->run() === TRUE )
						{
							$data['category'] = $this->input->post('category');
							$data['summary'] = $this->input->post('m_content');
							$data['place'] = $this->input->post('place');
							$data['begtime'] = strtotime($this->input->post('begtime'));
							$data['pro_num'] = intval($this->input->post('m_num'));
						}
						break;
					case '2':
						// 工作
						$this->form_validation->set_rules($workConfig);
						if ( $this->form_validation->run() === TRUE )
						{
							// 要求
// 							$data['summary'] = $this->input->post('sex');
// 							$data['summary'] .= '||' . $this->input->post('people_num');
// 							$data['summary'] .= '||' . $this->input->post('height');
// 							$data['summary'] .= '||' . $this->input->post('style');
							$data['summary'] = $this->input->post('summary');
							
							$data['category'] = $this->input->post('category');
							$data['place'] = $this->input->post('place');
							$data['begtime'] = strtotime($this->input->post('begtime'));
							$data['endtime'] = strtotime($this->input->post('endtime'));
							$data['content'] = $this->input->post('introduction');
						}
						break;
					case '1':
						// 面试
						$this->form_validation->set_rules($interviewConfig);
						if ( $this->form_validation->run() === TRUE )
						{
							// 要求
// 							$data['summary'] = $this->input->post('sex');
// 							$data['summary'] .= '||' . $this->input->post('people_num');
// 							$data['summary'] .= '||' . $this->input->post('height');
// 							$data['summary'] .= '||' . $this->input->post('style');
							$data['summary'] = $this->input->post('summary');
							
							$data['category'] = $this->input->post('category');
							$data['place'] = $this->input->post('address');
							$data['begtime'] = strtotime($this->input->post('interview_begin_time'));
							$data['content'] = $this->input->post('introduction');
						}
						break;
					default:
						break;
				}
				
				$id = _get_key_val($this->input->get('id'), TRUE);
				if ( $id )
				{
					$data['id'] = $id;
					// 更新
					$this->Activity_model->update_by_id($id, $data);
				} else
				{
					
					$data['userid'] = $this->thatUser['id'];
					$data['nickname'] = $this->thatUser['nickname'];
					$data['addtime'] = time();
					// 新增
					$this->Activity_model->insert($data);
				}
				
				redirect('/admin/activity');
				exit();
			}
		}

		if(!empty($id))
		{
// 			$oSysActType = _get_config('activity');
			$info = $this->Activity_model->get_info_by_id($id);
			// 处理时间
// 			if ( ! empty($info) && (is_numeric($info['begtime'])) )
// 			{
// 				$info['begtime'] = date('Y-m-d', (int) $info['begtime']);
// 			}
			$result = array(
				'info'=>$info,
				'oSysActType' => _get_config('activity'),
				'noticeArr'=>_get_config('work_cat')
				);
		}
		

		$this->load->view('admin/activity_add', $result);
	}

	public function save()
	{
		
		if ($this->input->is_post())
		{
			//验证规则
			$config = array(
//               array(
//                     'field'   => 'title', 
//                     'label'   => '活动名称', 
//                     'rules'   => 'trim|required'
//                  ),
//                array(
//                     'field'   => 'type', 
//                     'label'   => '活动类型', 
//                     'rules'   => 'trim|required'
//                  ),
//               	array(
//                     'field'   => 'img', 
//                     'label'   => '活动图片', 
//                     'rules'   => 'trim|required'
//                  ),  
//                array(
//                     'field'   => 'begtime', 
//                     'label'   => '开始时间', 
//                     'rules'   => 'trim|required'
//                  ),  
//                array(
//                     'field'   => 'endtime', 
//                     'label'   => '结束时间', 
//                     'rules'   => 'trim|required'
//                  ),  
//                array(
//                     'field'   => 'place', 
//                     'label'   => '地点', 
//                     'rules'   => 'trim|required'
 //                 ),  
                array(
                     'field'   => 'display', 
                     'label'   => '显示', 
                     'rules'   => 'trim|required'
                  ),  
            );

            $this->form_validation->set_rules($config);

			if ($this->form_validation->run() === TRUE)
  			{

  				$data = array(
					'title'=>$this->input->post('title'),
					'type'=>$this->input->post('type'),
					'img'=>$this->input->post('img'),
					'summary'=>$this->input->post('summary'),
					'workfee'=>$this->input->post('workfee'),
					'begtime'=>strtotime($this->input->post('begtime')),
					'endtime'=>strtotime($this->input->post('endtime')),
					'inendtime'=>strtotime($this->input->post('inendtime')),
					'place'=>$this->input->post('place'),
					'address'=>$this->input->post('address'),
					'actnum'=>(int)$this->input->post('actnum'),
					'innumfake'=>$this->input->post('innumfake'),
					'innum'=>$this->input->post('innum'),
					'addtime'=>time(),
					'display'=>$this->input->post('display'),
					'status'=>1,
					'op_userid'=>$this->session->userdata['admin_id'],
					'op_username'=>$this->session->userdata['user_name'],
					'op_time'=>time(),
				);

  				$id	= _get_key_val($this->input->get('id'), TRUE);
  				if($id)
  					$data['id'] = $id;

  				$this->Activity_model->insert($data);

				//echo '成功,<a href="/admin/aa">返回列表页</a>';
				redirect(base_url('/admin/activity'));
				exit;
  			}
  			else
  			{
  				$id	= _get_key_val($this->input->get('id'), TRUE);
				$result = array();

				if(!empty($id))
				{
					$info = $this->Activity_model->get_info_by_id($id);
					$result = array(
						'info'=>$info,
						);
				}
  				$this->load->view('admin/activity_add',$result);
  				//redirect('/admin/link/add?id='.$this->input->get('id'));
  			}
			



		}
	}

	function del(){
		$id	= _get_key_val($this->input->get('id'), TRUE);
		$page = _get_page();

		$this->Activity_model->delete_by_id($id);
		redirect( base_url('/admin/activity?page='.$page) );

	}

	function recommend(){
		$id	= _get_key_val($this->input->get('id'), TRUE);
		$page = _get_page();

		$this->load->model('Recommend_model');
		$this->Recommend_model->do_recommend(2,$id);

		redirect( base_url('/admin/activity?page='.$page) );

	}
	
	//互动总览推荐
	function fansrecommend(){
	    $id	= _get_key_val($this->input->get('id'), TRUE);
	    $page = _get_page();
	
	    $this->load->model('Fansrecommend_model');
	    $this->Fansrecommend_model->do_recommend(2,$id);
	
	    redirect( base_url('/admin/activity?page='.$page) );
	
	}
}
